package indi.zn.commons.tree;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.persistence.Transient;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * @author: 周楠
 * @date 2016年6月8日 上午9:09:48
 */

public abstract class TreeNode<I> implements Comparator<TreeNode<I>>{
	
	@Transient
	@JsonIgnore
	@JSONField(serialize = false)
	protected TreeNode<I> parentNode;
	@Transient
	protected List<TreeNode<I>>  childrenNodes;
	 
	public TreeNode(){}; 
	 
	public  TreeNode(I nid, I pid) {
		setNid(nid);
		setPid(pid);
		//this.nodeObject=nodeObject;
		childrenNodes=new ArrayList<>();
	}

	public abstract I getNid() ;

	public abstract void setNid(I nid) ;

	public abstract I getPid() ;

	public abstract void setPid(I pid);
	



	public List<TreeNode<I>> getChildrenNodes() {
		return childrenNodes;
	}

	public void setChildrenNodes(List<TreeNode<I>> childrenNodes) {
		this.childrenNodes = childrenNodes;
	}

	public TreeNode<I> getParentNode() {
		return parentNode;
	}

	public void setParentNode(TreeNode<I> parentNode) {
		this.parentNode = parentNode;
	}

	@Override
	public int compare(TreeNode<I> o1, TreeNode<I> o2) {
		// TODO Auto-generated method stub
		
		int res=0;
		
		 if(o1.getPid().getClass().getName().equals(Integer.class.getName())){
			 
			 Integer o1pid= (Integer) o1.getPid();
			 Integer o2pid=(Integer) o2.getPid();
			 
			 res=Integer.compare(o1pid, o2pid);
			 
		 }else if(o1.getPid().getClass().getName().equals(String.class.getName())){
			 String o1pid= (String) o1.getPid();
			 String o2pid=(String) o2.getPid();
			 res= Collator.getInstance().compare(o1pid, o2pid);
		 }else if(o1.getPid().getClass().getName().equals(Long.class.getName())){
			 Long o1pid= (Long) o1.getPid();
			 Long o2pid=(Long) o2.getPid();
			 res=Long.compare(o1pid, o2pid);
		 }
		
		return res;
	}
	 
	 
	
	@SuppressWarnings("rawtypes")
	public static class  NodeComparator implements Comparator<TreeNode> {

		public int compare(TreeNode o1, TreeNode o2) {
			// TODO Auto-generated method stub

			int res=0;
			
			 if(o1.getPid().getClass().getName().equals(Integer.class.getName())){
				 
				 Integer o1pid= (Integer) o1.getPid();
				 Integer o2pid=(Integer) o2.getPid();
				 
				 res=Integer.compare(o1pid, o2pid);
				 
			 }else if(o1.getPid().getClass().getName().equals(String.class.getName())){
				 String o1pid= (String) o1.getPid();
				 String o2pid=(String) o2.getPid();
				 res= Collator.getInstance().compare(o1pid, o2pid);
			 }else if(o1.getPid().getClass().getName().equals(Long.class.getName())){
				 Long o1pid= (Long) o1.getPid();
				 Long o2pid=(Long) o2.getPid();
				 res=Long.compare(o1pid, o2pid);
			 }
			 			
			return res;
		}
		
	}
	
	
	 

}
